package com.heqiang.utils;


import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;

@CrossOrigin
@RestController
public class BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public BaseController() {
    }


    public ModelAndView goView(String path) {
        return new ModelAndView(path);
    }

    public static String getIpAddr(HttpServletRequest request) {
        String ip = request.getHeader("X-Forwarded-For");
        if (ip != null && ip.length() != 0 && !"unknown".equalsIgnoreCase(ip)) {
            String[] ips = ip.split(",");

            for (int index = 0; index < ips.length; ++index) {
                String strIp = ips[index];
                if (!"unknown".equalsIgnoreCase(strIp)) {
                    ip = strIp;
                    break;
                }
            }

            return ip;
        } else {
            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_CLIENT_IP");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getHeader("HTTP_X_FORWARDED_FOR");
            }

            if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }

            return ip;
        }
    }

    public static boolean isWeixinBrowser(HttpServletRequest request) {
        if (StringUtils.isBlank(request.getHeader("user-agent"))) {
            return false;
        } else {
            return request.getHeader("user-agent").toLowerCase().indexOf("micromessenger") > 0;
        }
    }

    public static String genErrorMsg(String msg, Exception e) {
        if (e == null) {
            return msg;
        } else {
            StringBuilder sb = new StringBuilder(msg + "\t" + e.getMessage());
            StackTraceElement[] sts = e.getStackTrace();
            if (sts != null && sts.length >= 2) {
                sb.append("\n[ASSERT TRACE - " + e.getMessage() + "] - ");
                sb.append(sts[1].getClassName()).append(".").append(sts[1].getMethodName()).append("@").append(sts[1].getLineNumber());
                return sb.toString();
            } else {
                return sb.toString();
            }
        }
    }

    public static String getAllPath(HttpServletRequest request) {
        return request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + request.getContextPath();
    }
}
